// import { addTodo } from '@/api/Api.js'
import * as Actions from '../Actions'

const initState = {
	permission: {
		tree: [],
		data: {},
	},
	roles: [],
	users: {
		'count': 0,
		'list': [],
	},
}

const Permission = (state = initState, action) => {
	switch (action.type) {
		case Actions.setPermission:
			return {
				...state,
				permission: action.permission
			};
		case Actions.setUsers:
			return {
				...state,
				users: action.users
			};
		case Actions.deleteUsers:
			let list = state.users.list;
			list.splice(action.index, 1);
			return {
				...state,
				users: {
					list: [
						...list
					]
				}
			};
			return {
				...state,
				users: action.users
			};
		case Actions.addUsers:
			return {
				...state,
				users: {
					// count: state.users.count,
					list: [
						action.user,
						...state.users.list,
					]
				}
			};
		case Actions.updateUsers:
			let userlist = state.users.list;
			userlist[ action.index ] = action.user;
			return {
				...state,
				users: {
					// count: state.users.count,
					list: [...userlist]
				},
			};
		case Actions.setRoles:
			return {
				...state,
				roles: action.roles,
			};
		case Actions.updateRoles:
			let roles = state.roles;
			roles[ action.index ] = action.role;
			return {
				...state,
				roles: [...roles],
			};
		/**
		 * 添加角色
		 */
		case Actions.addRoles:
			return {
				...state,
				roles: [
					action.role,
					...state.roles,
				]
			};
		case Actions.deleteRoles:
			let newRoles = state.roles;
			newRoles.splice(action.index, 1);
			return {
				...state,
				roles: [
					...newRoles,
				]
			};
		default:
			return state;
	}
}

export default Permission;